package cn.tank.data.sync.redis;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.tank.data.sync.core.MessageSendService;
import cn.tank.data.sync.core.common.Message;
import com.alibaba.fastjson.JSONObject;
import org.springframework.data.redis.core.RedisTemplate;

/************************************************
 * @Description:Redis消息发送器
 * @Version: 1.0
 ************************************************/

public class RedisMessagePublisher implements MessageSendService {

    public final RedisTemplate redisTemplate;

    public RedisMessagePublisher(RedisTemplate redisTemplate) {
        Assert.notNull(redisTemplate);

        this.redisTemplate = redisTemplate;
    }

    @Override
    public void sendMessage(String channel, Message messge) {
        if (StrUtil.isNotEmpty(channel)) {
            redisTemplate.convertAndSend(channel, messge == null ? "" : JSONObject.toJSONString(messge));
        }
    }
}
